package br.com.classificadosonline.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class AreaRestritaFilter
 */
public class AreaRestritaFilter implements Filter {

    /**
     * Default constructor. 
     */
    public AreaRestritaFilter() {
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest)request; 
		HttpSession session = httpRequest.getSession();
		
		Boolean autenticado = (Boolean)session.getAttribute("autenticado");
		
		if (autenticado == null || !autenticado) {
			HttpServletResponse httpResponse = (HttpServletResponse)response;
			// FIXME trocar por contexto dinamico
			httpResponse.sendRedirect("ClassificadosOnline/index.jsp");
		} else {
			chain.doFilter(request, response);
		}
		
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
	}

}
